package org.sunshine.miracle.ui;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.program.Program;
import org.sunshine.miracle.Commons;
import org.sunshine.miracle.model.VirtualFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class ImageCache {

    private static Map<String, Image> imageCache = new HashMap<String, Image>();

    public static String FILE = "File.gif";
    public static String CLOSED_REAL_FOLDER = "ClosedFolder.gif";
    public static String OPEN_REAL_FOLDER = "OpenFolder.gif";
    public static String CLOSED_VIRTUAL_FOLDER = "ClosedVirtualFolder.gif";
    public static String OPEN_VIRTUAL_FOLDER = "OpenVirtualFolder.gif";


    public static synchronized Image getImage(String name) {
        Image image = imageCache.get(name);
        if (image != null) return image;
        try {
            image = createImage(name);
            if (image!=null){
                imageCache.put(name, image);
                return image;
            }
        } catch (IOException e) {
            throw new IllegalStateException("Cannot get image at '" + name + "'", e);
        }
        return getImage(FILE); // return default image;
    }


    private static Image createImage(String name) throws IOException {
        InputStream inputStream = ImageCache.class.getResourceAsStream("/org/sunshine/miracle/ui/images/" + name);
        if (inputStream == null) {
            inputStream = Commons.getFileAsInputStream(name);
        }
        if (inputStream == null) return null;

        ImageData imageData = new ImageData(inputStream);
        ImageData mask = imageData.getTransparencyMask();
        Image result = new Image(SWTUtils.getInstance().display, imageData, mask);
        inputStream.close();

        return result;
    }

    /** Frees the resources */
    public static void freeResources() {
        if (imageCache != null) {
            for (Image image : imageCache.values()) {
                image.dispose();
            }
        }
        imageCache = new HashMap<String, Image>();
    }


    /**
     * Get image assosiates to given file extension. If there are no such in the cache,
     * it try to get the image from assosiated program and save image in the cache.
     */
    public static Image getImageForFile(VirtualFile file) {
        int dot = file.getName().lastIndexOf('.');
        if (dot != -1) {
            String extension = file.getName().substring(dot);
            Program program = Program.findProgram(extension);

            if (program != null) {
                Image image = imageCache.get(program.getName());
                if (image != null) {
                    return image;
                }

                ImageData imageData = program.getImageData();
                image = new Image(null, imageData, imageData.getTransparencyMask());
                imageCache.put(program.getName(), image);
                return image;
            }
        }
        return getImage(FILE);// return default file image
    }
}